/*     */ package dm.jdbc.driver;
/*     */ 
/*     */ import dm.jdbc.dbaccess.DBError;
/*     */ import dm.jdbc.dbaccess.DmMsgSend;
/*     */ import dm.jdbc.desc.ParameterDesc;
/*     */ import dm.sql.TypeData;
/*     */ import dm.sql.TypeDescriptor;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.sql.SQLException;
/*     */ 
/*     */ 
/*     */ public class DmInputStreamBinder
/*     */   extends DmBinder
/*     */ {
/*  16 */   boolean toBindLobCyc = false;
/*  17 */   boolean new_lob_flag = false;
/*  18 */   byte[] ret = null;
/*  19 */   int retLen = 0;
/*     */   
/*     */   public DmInputStreamBinder(DmdbPreparedStatement_bs pstmt) {
/*  22 */     super(pstmt);
/*  23 */     this.new_lob_flag = pstmt.getConnection_bs().getNewLobFlag();
/*     */   }
/*     */   
/*     */   void init() {
/*  27 */     this.recDType = 12;
/*  28 */     this.recPrec = Integer.MAX_VALUE;
/*  29 */     this.recScale = 0;
/*     */   }
/*     */   
/*     */   void checkType(int icol, boolean forBatch) throws SQLException {
/*  33 */     if (this.m_checkType) {
/*  34 */       return;
/*     */     }
/*  36 */     this.m_checkType = true;
/*     */     
/*  38 */     if (this.paramsDesc == null) {
/*  39 */       this.paramsDesc = this.pstmt.getParamsDesc();
/*     */     }
/*  41 */     byte typeFlag = this.paramsDesc[icol].getTypeFlag();
/*  42 */     int dType = this.paramsDesc[icol].getDType();
/*  43 */     int prec = this.paramsDesc[icol].getPrec();
/*  44 */     int scale = this.paramsDesc[icol].getScale();
/*  45 */     byte ioType = this.paramsDesc[icol].getIOType();
/*     */     
/*  47 */     this.destIoType = ioType;
/*  48 */     this.srcScale = scale;
/*     */     
/*  50 */     if ((typeFlag == 1) || 
/*  51 */       (ioType == 1)) {
/*  52 */       this.destDType = dType;
/*  53 */       this.destPrec = prec;
/*  54 */       this.destScale = scale;
/*  55 */       return;
/*     */     }
/*     */     
/*  58 */     switch (dType) {
/*     */     case 0: 
/*     */     case 1: 
/*     */     case 2: 
/*     */     case 17: 
/*     */     case 18: 
/*  64 */       this.destDType = 18;
/*  65 */       this.destPrec = 8188;
/*  66 */       this.destScale = 0;
/*  67 */       break;
/*     */     
/*     */     case 12: 
/*     */     case 19: 
/*  71 */       this.destDType = 12;
/*  72 */       this.destPrec = Integer.MAX_VALUE;
/*  73 */       this.destScale = 0;
/*  74 */       break;
/*     */     
/*     */     default: 
/*  77 */       DBError.throwSQLException(6007);
/*     */     }
/*     */   }
/*     */   
/*     */   private byte[] readLeftCompletelyWithTry(DmInputStreamParam inParm)
/*     */     throws IOException
/*     */   {
/*  84 */     InputStream in = inParm.m_in;
/*  85 */     long targetLen = inParm.m_targetLen;
/*     */     
/*  87 */     byte[] ret = null;
/*  88 */     if (this.streamReadOver)
/*     */     {
/*  90 */       ret = new byte[this.streamReadTryLen];
/*  91 */       System.arraycopy(this.streamReadTry, 0, ret, 0, this.streamReadTryLen);
/*  92 */       return ret;
/*     */     }
/*     */     
/*  95 */     int maxLen = 32000;
/*  96 */     byte[] totalBuf = new byte[maxLen];
/*     */     
/*  98 */     int count = 0;
/*     */     do {
/* 100 */       if (count >= maxLen) {
/* 101 */         maxLen += 16000;
/*     */         
/* 103 */         byte[] totalTemp = new byte[maxLen];
/* 104 */         System.arraycopy(totalBuf, 0, totalTemp, 0, count);
/* 105 */         totalBuf = totalTemp;
/*     */       }
/*     */       
/* 108 */       int readLen = in.read(totalBuf, count, maxLen - count);
/* 109 */       if (readLen <= 0) {
/*     */         break;
/*     */       }
/* 112 */       count += readLen;
/* 113 */     } while ((targetLen == -1L) || (count + this.streamReadTryLen < targetLen));
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 118 */     if (targetLen != -1L) {
/* 119 */       ret = new byte[(int)targetLen];
/* 120 */       System.arraycopy(this.streamReadTry, 0, ret, 0, this.streamReadTryLen);
/* 121 */       System.arraycopy(totalBuf, 0, ret, this.streamReadTryLen, 
/* 122 */         (int)(targetLen - this.streamReadTryLen));
/*     */     } else {
/* 124 */       ret = new byte[count + this.streamReadTryLen];
/* 125 */       System.arraycopy(this.streamReadTry, 0, ret, 0, this.streamReadTryLen);
/* 126 */       System.arraycopy(totalBuf, 0, ret, this.streamReadTryLen, count);
/*     */     }
/*     */     
/* 129 */     this.streamReadOver = true;
/*     */     
/* 131 */     return ret;
/*     */   }
/*     */   
/*     */   public int bindData(DmMsgSend msg, int irow, int icol, boolean forBatch) throws SQLException
/*     */   {
/* 136 */     DmInputStreamParam inParam = ((DmInputStreamParam[])this.pstmt.m_paramObjects[icol])[irow];
/* 137 */     InputStream in = inParam.m_in;
/* 138 */     long targetLen = inParam.m_targetLen;
/* 139 */     TypeDescriptor typeDesc = this.paramsDesc[icol].getTypeDescriptor();
/*     */     
/* 141 */     if (this.toBindLobCyc) {
/* 142 */       return -1;
/*     */     }
/*     */     
/* 145 */     if (this.ret != null) {
/* 146 */       int realLen = msg.appendBytesWithLen2(this.ret, 0, this.retLen);
/* 147 */       this.ret = null;
/* 148 */       this.retLen = 0;
/* 149 */       return realLen;
/*     */     }
/*     */     try
/*     */     {
/* 153 */       int maxLen = 16000;
/*     */       
/* 155 */       this.streamReadTry = new byte[maxLen];
/* 156 */       this.streamReadTryLen = in.read(this.streamReadTry, 0, maxLen);
/* 157 */       if (this.streamReadTryLen < maxLen) {
/* 158 */         if (this.streamReadTryLen == -1) {
/* 159 */           this.streamReadTryLen = 0;
/*     */         }
/* 161 */         this.streamReadOver = true;
/*     */       }
/*     */       
/* 164 */       this.retLen = this.streamReadTryLen;
/* 165 */       if ((targetLen != -1L) && (this.retLen > targetLen)) {
/* 166 */         this.retLen = ((int)targetLen);
/* 167 */         this.streamReadOver = true;
/*     */       }
/*     */       
/* 170 */       if (((this.destDType == 12) || 
/* 171 */         (this.destDType == 19)) && 
/* 172 */         (this.retLen > 2048)) {
/* 173 */         this.toBindLobCyc = true;
/* 174 */         return -1;
/*     */       }
/*     */       
/*     */ 
/* 178 */       switch (this.destDType) {
/*     */       case 0: 
/*     */       case 1: 
/*     */       case 2: 
/*     */       case 12: 
/*     */       case 17: 
/*     */       case 18: 
/*     */       case 19: 
/*     */       case 119: 
/* 187 */         if (this.streamReadOver) {
/* 188 */           this.ret = this.streamReadTry;
/*     */         } else {
/* 190 */           this.ret = readLeftCompletelyWithTry(inParam);
/* 191 */           this.retLen = this.ret.length;
/*     */         }
/*     */         
/* 194 */         if ((this.destDType == 119) && (typeDesc != null)) {
/* 195 */           this.ret = TypeData.objBlobToBytes(this.ret, typeDesc);
/* 196 */           this.retLen = this.ret.length;
/*     */         }
/*     */         
/* 199 */         break;
/*     */       default: 
/* 201 */         DBError.throwSQLException(6007);
/*     */       }
/*     */     }
/*     */     catch (IOException e) {
/* 205 */       throw new SQLException(e.getMessage());
/*     */     }
/*     */     
/* 208 */     clearVars();
/* 209 */     return msg.appendBytesWithLen2(this.ret, 0, this.retLen);
/*     */   }
/*     */   
/*     */   public int bindLobCyc(DmMsgSend lobMsg, int irow, int icol) throws SQLException
/*     */   {
/* 214 */     DmInputStreamParam inParam = ((DmInputStreamParam[])this.pstmt.m_paramObjects[icol])[irow];
/* 215 */     InputStream in = inParam.m_in;
/* 216 */     long targetLen = inParam.m_targetLen;
/*     */     
/* 218 */     int realLen = 0;
/* 219 */     int retLen = 0;
/*     */     
/* 221 */     this.toBindLobCyc = false;
/*     */     
/* 223 */     if (this.streamReadTry != null) {
/* 224 */       realLen = this.streamReadTryLen;
/* 225 */       if ((targetLen != -1L) && (this.streamReadTryLen > targetLen)) {
/* 226 */         realLen = (int)targetLen;
/*     */       }
/* 228 */       retLen = lobMsg.appendOffRowBytes(this.streamReadTry, 0, realLen, 
/* 229 */         this.new_lob_flag, -1);
/* 230 */       this.streamReadTry = null;
/* 231 */       return retLen;
/*     */     }
/*     */     
/* 234 */     if (this.streamReadOver) {
/* 235 */       clearVars();
/* 236 */       return 0;
/*     */     }
/*     */     
/* 239 */     int maxLen = 16000;
/* 240 */     byte[] ret = new byte[maxLen];
/*     */     try
/*     */     {
/* 243 */       retLen = in.read(ret, 0, maxLen);
/* 244 */       if (retLen == -1) {
/* 245 */         clearVars();
/* 246 */         return 0;
/*     */       }
/* 248 */       if (retLen < maxLen) {
/* 249 */         this.streamReadOver = true;
/*     */       }
/* 251 */       this.streamReadTryLen += retLen;
/* 252 */       if ((targetLen != -1L) && (this.streamReadTryLen >= targetLen)) {
/* 253 */         this.streamReadOver = true;
/* 254 */         retLen = (int)(retLen - (this.streamReadTryLen - targetLen));
/*     */       }
/*     */     }
/*     */     catch (IOException e) {
/* 258 */       throw new SQLException(e.getMessage());
/*     */     }
/*     */     
/* 261 */     return lobMsg.appendOffRowBytes(ret, 0, retLen, this.new_lob_flag, -1);
/*     */   }
/*     */   
/*     */   public void clearRetValue() {
/* 265 */     this.ret = null;
/* 266 */     this.retLen = 0;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\driver\DmInputStreamBinder.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */